﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{
    internal class ProductsProvider : ProductsBaseProvider
    {

        internal ProductsProvider(ObSession obSession, int productId)
            : base(obSession, productId) { }

        internal ProductsProvider() { }

        internal int InsertUpdateProduct(Product_Product product, ObSession obSession)
        {
            var res = 0;
            if ((ProfileType)obSession.IdProfile == ProfileType.Admin)
            {
                var query = _db.Product_Product.Where(w => w.Id == product.Id);
                if (query.Count() > 0) // Update
                {
                    query.First().AlturaPlanta = product.AlturaPlanta;
                    query.First().Color = product.Color;
                    query.First().DescripcionCorta = product.DescripcionCorta;
                    query.First().DescripcionLarga = product.DescripcionLarga;
                    query.First().Detalles = product.Detalles;
                    query.First().IsDestacado = product.IsDestacado;
                    query.First().IsOferta = product.IsOferta;
                    query.First().Mantenimiento = product.Mantenimiento;
                    query.First().MesesFloracion = product.MesesFloracion;
                    query.First().MesesFruto = product.MesesFruto;
                    query.First().Nombre = product.Nombre;
                    query.First().NombreCientifico = product.NombreCientifico;
                    query.First().Plantación = product.Plantación;
                    query.First().ShowAtHome = product.ShowAtHome;
                }
                else
                {
                    Int32 productId = 0;
                    productId = CompiledQuerys.GetProductId(_db, productId);
                    product.Id = productId;
                    _db.Product_Product.InsertOnSubmit(product);
                }
                _db.SubmitChanges();
                res = product.Id;
            }
            return res;
        }
        internal override void DeleteProduct()
        {
            if ((ProfileType)_obSession.IdProfile == ProfileType.Admin)
            {
                this.DeleteStep1();
                base.DeleteStep2();
                base.DeleteStep3();
                base.DeleteStep4();
                base.DeleteStep5();
                base.DeleteStep6();
            }

        }
        internal override void DeleteStep1()
        {
            var query = _db.Product_Product.Where(w => w.Id == this._productId);
            if (query.Count() > 0)
            {
                _db.Product_Product.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }
        }
        internal List<object> GetProductsSimpleListBySubCategory(int idSubCategory)
        {
            var res = new List<object>();
            var query = from b in _db.spGetProductsBySubCategoryAdmin(idSubCategory)
                        select new
                        {
                            Id = b.Id,
                            Name = b.Name,
                            url = b.url
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    Name = item.Name,
                    url = item.url
                });
            }
            return res;
        }
        internal List<object> GetAllProductsAdmin()
        {
            var res = new List<object>();
            var query = from b in _db.spGetAllProductsAdmin()
                        select new
                        {
                            Id = b.Id,
                            Name = b.Name,
                            url = b.url
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    Name = item.Name,
                    url = item.url
                });
            }
            return res;
        }
        internal List<ADProductSimple> GetAllProducts()
        {
            return CompiledQuerys.GetAllProducts(_db);
        }
        internal List<Product_Product> GetAllProductsFull()
        {
            var query = _db.Product_Product.Select(w => w);
            return query.Count() > 0 ? query.ToList() : new List<Product_Product>();
        }
        internal Product_Product GetProductId(int id)
        {
            var res = new Product_Product();
            var query = _db.Product_Product.Where(w => w.Id == id);
            res = query.FirstOrDefault<Product_Product>();
            return res;
        }

        internal void InsertUpdateProductPreferences(Product_ProductPreferenceGroup productPreferencegp)
        {

            var query = _db.Product_ProductPreferenceGroup.Where(w =>
                w.Product_PreferenceId == productPreferencegp.Product_PreferenceId &&
                w.Product_ProductId == productPreferencegp.Product_ProductId);
            if (query.Count() > 0)
            {
                query.First().Description = productPreferencegp.Description;
                query.First().Name = productPreferencegp.Name;
                query.First().Order = productPreferencegp.Order;
                query.First().PictureGuidId = productPreferencegp.PictureGuidId;
            }
            else
            {
                _db.Product_ProductPreferenceGroup.InsertOnSubmit(productPreferencegp);
            }
            _db.SubmitChanges();
        }
        internal void DeleteProductPreferences(int id)
        {
            var query = _db.Product_ProductPreferenceGroup.Where(w => w.Id == id);
            if (query.Count() > 0)
            {
                _db.Product_ProductPreferenceGroup.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }
        }
        internal List<object> GetProductPreferences(Int32 id)
        {
            var res = new List<object>();
            var query = from b in _db.spGetProductPreferences(id)
                        select new
                        {
                            Id = b.Id,
                            Name = b.Name,
                            Description = b.Description,
                            Order = b.Order,
                            url = b.url
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    Name = item.Name,
                    Description = item.Description,
                    Order = item.Order,
                    url = item.url
                });
            }
            return res;

        }
        internal UIProductPriceDetail GetProductPriceDetail(Int32 idProductPriceDetail, Int32 productId)
        {
            var result = new UIProductPriceDetail();
            var query = _db.spGetProductPriceDetail(idProductPriceDetail, productId).ToList();
            foreach (var item in query)
            {
                result.Description = item.Description;
                result.DiscountPrice = item.DiscountPrice;
                result.GuidId = "";
                result.Id = item.Id;
                result.ImageUrlThumnail = item.ImageUrlThumnail;
                result.IsAcompanamiento = item.IsAcompanamiento;
                result.Price = item.Price;
                result.ProductCode = item.ProductCode;
                result.Stock = item.Stock;
                break;
            }
            return result;
        }

    }
}
